# -*- coding: utf-8 -*-
# @Time     : 2021/12/1 15:54
# @Author   : wljess
# @File     : 多线程单例模式加锁.py
# @Describe : 
# @Software : PyCharm

import time
from threading import Thread, Lock


class Singleton:

    _lock = Lock()

    def __init__(self, x, y):
        #print("__init__", self)
        self.x = x
        self.y = y

    def __new__(cls, *args, **kwargs):
        with cls._lock:
            if not hasattr(cls, "_instance"):

                time.sleep(1)
                cls._instance = super().__new__(cls)
            #print("__new__", cls._instance)
            return cls._instance


def task(name, num):
    o = Singleton(num, num*2)
    print(f"创建对象{name}：{o}")


if __name__ == '__main__':
    lst_t = []
    for i in range(10):
        t = Thread(target=task, args=(i, i))
        t.start()


